package org.ksoap2.serialization;

import java.io.IOException;
import java.math.BigDecimal;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlSerializer;

public class MarshalFloat
  implements Marshal
{
  public Object readInstance(XmlPullParser parser, String namespace, String name, PropertyInfo propertyInfo)
    throws IOException, XmlPullParserException
  {
    Object result;
    String stringValue = parser.nextText();

    if (name.equals("float"))
      result = new Float(stringValue);
    else if (name.equals("double"))
      result = new Double(stringValue);
    else if (name.equals("decimal"))
      result = new BigDecimal(stringValue);
    else
      throw new RuntimeException("float, double, or decimal expected");

    return result;
  }

  public void writeInstance(XmlSerializer writer, Object instance) throws IOException {
    writer.text(instance.toString());
  }

  public void register(SoapSerializationEnvelope cm) {
    cm.addMapping(cm.xsd, "float", Float.class, this);
    cm.addMapping(cm.xsd, "double", Double.class, this);
    cm.addMapping(cm.xsd, "decimal", BigDecimal.class, this);
  }
}
